#pragma once
#include <Windows.h>
#include <wincodec.h>
#include <DWrite.h>
#include <DXGIType.h>
#include <cmath>
#include <vector>
#include <string>
#include <D2D1.h>
#include <d2d1_3.h>
#include <mutex>
#include "nanosvg.h"
#include "AutoFree.h"
#include "Convert.h"

class StaticObjects
{
public:
    static ID2D1Factory *D2DFactory()
    {
        static ID2D1Factory *pD2DFactory = nullptr;
        if (!pD2DFactory)
            D2D1CreateFactory(D2D1_FACTORY_TYPE_MULTI_THREADED, &pD2DFactory);
        return pD2DFactory;
    }
    static IDWriteFactory *DWriteFactory()
    {
        static IDWriteFactory *pDWriteFactory = nullptr;
        if (!pDWriteFactory)
            DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), reinterpret_cast<IUnknown **>(&pDWriteFactory));
        return pDWriteFactory;
    }
    static IWICImagingFactory *ImageFactory()
    {
        static IWICImagingFactory *_pImageFactory = nullptr;
        if (!_pImageFactory)
        {
            auto m = LoadLibraryA("Ole32.dll");
            if (m)
            {
                decltype(CoInitialize) *f = (decltype(CoInitialize) *)GetProcAddress(m, "CoInitialize");
                decltype(CoCreateInstance) *f1 = (decltype(CoCreateInstance) *)GetProcAddress(m, "CoCreateInstance");
                auto hr = f(NULL);
                hr = f1(CLSID_WICImagingFactory1, NULL, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, (LPVOID *)&_pImageFactory);
            }
        }
        return _pImageFactory;
    }
};
#define _D2DFactory StaticObjects::D2DFactory()
#define _DWriteFactory StaticObjects::DWriteFactory()
#define _ImageFactory StaticObjects::ImageFactory()